package edu.uoc.tds.pac4.client.ss2;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.swing.JPanel;

import edu.uoc.tds.pac4.beans.Event;
import edu.uoc.tds.pac4.client.PanelClientInterface;
import edu.uoc.tds.pac4.client.ss2.GestorPantallaSS2.TipusPantalla;
import edu.uoc.tds.pac4.common.GestioEventsException;
import edu.uoc.tds.pac4.common.PantallaError;
import edu.uoc.tds.pac4.common.TDSLanguageUtils;
import edu.uoc.tds.pac4.interfaces.RemoteInterface;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumn;

/**
 * Pantalla que mostra els assistens a un event finalitzat 
 */
public class PantallaAssistentsEvent implements PanelClientInterface {
	
	private static final long serialVersionUID = 1L;
	protected RemoteInterface remot;
	private Event event = null;
	private GestorPantallaSS2 gestor = null;
	private JPanel jPanelContainer = null;  //  @jve:decl-index=0:visual-constraint="223,78"
	private JLabel jLabelTitolEvent = null;
	private JLabel jLabelNomEvent = null;
	private JPanel jPanelTitolEvent = null;
	private JScrollPane jScrollPaneAssistents = null;
	private JTable jTableAssistents = null;
	private JPanel jPanelAccions = null;
	private JButton jButtonTornar = null;
	private JLabel jLabelTitolData = null;
	private JLabel jLabelDataEvent = null;
	private TableAssistentsModel tableModel;
	/**
	 * Constructor
	 * @param remote
	 * @param gestorPantallaSS2
	 * @param event
	 */
	public PantallaAssistentsEvent(RemoteInterface remot, GestorPantallaSS2 gestor, Event event) {
		super();
		this.remot = remot;
		this.gestor = gestor;
		this.event = event;
	}

	/**
	 * This method initializes jPanelContainer1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	@Override
	public JPanel getJPanelContainer(Dimension dimension) {
		if (jPanelContainer == null) {
			GridBagConstraints gbcPanelAccions = new GridBagConstraints();
			gbcPanelAccions.gridx = 0;
			gbcPanelAccions.gridy = 2;
			
			GridBagConstraints gbcPanelAssistents = new GridBagConstraints();
			gbcPanelAssistents.fill = GridBagConstraints.BOTH;
			gbcPanelAssistents.gridy = 1;
			gbcPanelAssistents.gridx = 0;
			gbcPanelAssistents.weightx = 1.0;
			gbcPanelAssistents.weighty = 1.0;
			
			GridBagConstraints gbcPanelTitol = new GridBagConstraints();
			gbcPanelTitol.gridx = 0;
			gbcPanelTitol.gridy = 0;
			
			jPanelContainer = new JPanel();
			jPanelContainer.setLayout(new GridBagLayout());
			jPanelContainer.setSize(new Dimension(670, 355));
			jPanelContainer.add(getJPanelTitolEvent(), gbcPanelTitol);
			jPanelContainer.add(getJScrollPaneAssistents(), gbcPanelAssistents);
			jPanelContainer.add(getJPanelAccions(), gbcPanelAccions);
		}
		return jPanelContainer;
	}

	/**
	 * This method initializes jPanelTitolEvent	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelTitolEvent() {
		if (jPanelTitolEvent == null) {
			DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
			
			GridBagConstraints gbcLabelDataEvent = new GridBagConstraints();
			gbcLabelDataEvent.gridx = 1;
			gbcLabelDataEvent.gridy = 1;
			gbcLabelDataEvent.insets = new Insets(5, 20, 15, 20);
			jLabelDataEvent = new JLabel();
			jLabelDataEvent.setText(formatter.format(event.getDataInici())
					+ " - " + formatter.format(event.getDataFi()));
			
			GridBagConstraints gbcLabelTitolData = new GridBagConstraints();
			gbcLabelTitolData.gridx = 1;
			gbcLabelTitolData.gridy = 0;
			gbcLabelTitolData.insets = new Insets(5, 20, 5, 20);
			jLabelTitolData = new JLabel();
			jLabelTitolData.setText(TDSLanguageUtils.getMessage("Client.PantallaAssistentsEvents.DatesCelebracio"));
			
			GridBagConstraints gbcLabelNomEvent = new GridBagConstraints();
			gbcLabelNomEvent.gridx = 0;
			gbcLabelNomEvent.gridy = 1;
			gbcLabelNomEvent.insets = new Insets(5, 20, 15, 20);
			jLabelNomEvent = new JLabel();
			jLabelNomEvent.setText(event.getDescripcio());
			
			GridBagConstraints gbcLabelTitolEvent = new GridBagConstraints();
			gbcLabelTitolEvent.gridx = 0;
			gbcLabelTitolEvent.gridy = 0;
			gbcLabelTitolEvent.insets = new Insets(5, 20, 5, 20);
			jLabelTitolEvent = new JLabel();
			jLabelTitolEvent.setText(TDSLanguageUtils.getMessage("Client.PantallaAssistentsEvents.NomEvent"));
			
			jPanelTitolEvent = new JPanel();
			jPanelTitolEvent.setLayout(new GridBagLayout());
			jPanelTitolEvent.add(jLabelTitolEvent, gbcLabelTitolEvent);
			jPanelTitolEvent.add(jLabelNomEvent, gbcLabelNomEvent);
			jPanelTitolEvent.add(jLabelTitolData, gbcLabelTitolData);
			jPanelTitolEvent.add(jLabelDataEvent, gbcLabelDataEvent);
		}
		return jPanelTitolEvent;
	}

	/**
	 * This method initializes jScrollPaneAssistents	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPaneAssistents() {
		if (jScrollPaneAssistents == null) {
			jScrollPaneAssistents = new JScrollPane();
			jScrollPaneAssistents.setViewportView(getJTableAssistents());
		}
		return jScrollPaneAssistents;
	}

	/**
	 * This method initializes jTableAssistents	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getJTableAssistents() {
		if (jTableAssistents == null) {
			tableModel = new TableAssistentsModel();
			
			try {
				tableModel.addParticipacions(remot.getParticipacions(event, true));
			} catch (RemoteException e) {
				new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
			} catch (GestioEventsException e) {
				new PantallaError(e.getMessage(), PantallaError.Tipus.ERROR);
			}
			
			jTableAssistents = new JTable(tableModel);
			jTableAssistents.setAutoCreateRowSorter(true);
			jTableAssistents.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			jTableAssistents.setRowSelectionAllowed(false);
			jTableAssistents.addMouseListener(new MouseAdapter(){
				public void mouseClicked(MouseEvent e)
				{
					// Comprova si es canvia l'estat del checkbox
					int col = jTableAssistents.getColumnCount() - 1;
					if ( jTableAssistents.getSelectedColumn() == col)
					{
						int row = jTableAssistents.getSelectedRow();
						
						// Canvi de l'estat de assistencia
						boolean assistencia = (Boolean)jTableAssistents.getValueAt(row, col);
						String codi_usuari = (String) jTableAssistents.getValueAt(row, 0);
						try {
							remot.actulitzaAssistencia(event.getCodi(), codi_usuari, assistencia);
						} catch (RemoteException ex) {
							// reverteix l'estat del checkbox
							jTableAssistents.setValueAt(!assistencia, row, col);
							new PantallaError("Remote.Error", PantallaError.Tipus.ERROR);
						} catch (GestioEventsException ex) {
							jTableAssistents.setValueAt(!assistencia, row, col);
							new PantallaError(ex.getMessage(), PantallaError.Tipus.ERROR);
						}
					}
				}
			} );
			
			// column width
			TableColumn tableColumn;
			tableColumn = jTableAssistents.getColumn(TDSLanguageUtils
					.getMessage("Client.PantallaAssistentsEvents.Assistencia"));
			tableColumn.setPreferredWidth(70);
			tableColumn.setMaxWidth(90);
		}
		return jTableAssistents;
	}
	
	/**
	 * This method initializes jPanelAccions	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelAccions() {
		if (jPanelAccions == null) {
			GridBagConstraints gbcButton = new GridBagConstraints();
			gbcButton.insets = new Insets(20, 10, 20, 10);
			
			jPanelAccions = new JPanel();
			jPanelAccions.setLayout(new GridBagLayout());
			jPanelAccions.add(getJButtonTornar(), gbcButton);
		}
		return jPanelAccions;
	}

	/**
	 * This method initializes jButtonTornar	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonTornar() {
		if (jButtonTornar == null) {
			jButtonTornar = new JButton();
			jButtonTornar.setText(TDSLanguageUtils.getMessage("Client.PantallaAssistentsEvents.Tornar"));
			jButtonTornar.setPreferredSize(new Dimension(125, 30));
			jButtonTornar.setMinimumSize(new Dimension(125, 30));
			jButtonTornar.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					gestor.carregaPantalla(TipusPantalla.llistar, null);
				}
			});
		}
		return jButtonTornar;
	}
}
